 var checkmatrix;
 var global;
 var isSave = false;
 $('#display').delegate('span.MathJax', 'click', function () {
     var scripts = $(this).next();
     var revertMathquill = scripts.html();
     revertMathquill = revertMathquill.replace(new RegExp("\\\\limits", 'g'), '\limits');
	 revertMathquill = revertMathquill.replace(new RegExp("begin{cases}", 'g'), 'hept{\\begin{cases}');
	 revertMathquill = revertMathquill.replace(new RegExp("end{cases}", 'g'), 'end{cases}}');
   /*  var mapObj = {
         vmatrix: "matrix",
         cases: "matrix"
     };
	 
     var re = new RegExp(Object.keys(mapObj).join("|"), "gi");
     revertMathquill = revertMathquill.replace(re, function (matched) {
         return mapObj[matched];
     });*/
     //matheditor.insert(revertMathquill);
	 $('#editable-math').mathquill('write', revertMathquill);
	 revertMathquill = revertMathquill.replace(new RegExp("limits", 'g'), '\\limits');
     $('#latex-source').val(revertMathquill);
 });

 function editMath() {
     if (isSave == true) {
         $('#display').attr('contenteditable', 'true');
         $('#display').html(global);
         isSave = false;
     }
 }

 function saveMath() {
         if (isSave == false) {
             var math = document.getElementById("display");
             global = $('#display').html();
             MathJax.Hub.Queue(["Typeset", MathJax.Hub, math]);
             $('#display').removeAttr('contenteditable');
             isSave = true;

         }
     }
     //	

 function passValue(i) {

         var latex = $('#latex-source').val();
         var el = document.getElementById("display");
         if (latex != '' && isSave == false) {
				latex = latex.replace(new RegExp('\\\\hept{', 'g'), '');
				latex = latex.replace(new RegExp('cases}}', 'g'), 'cases}');
             var mapObj = {
                 prod: "prod\\limits",
                 coprod: "coprod\\limits",
                 cos: "cos\\limits",            
                 tan: "tan\\limits",
                 cot: "cot\\limits",
                 sin: "sin\\limits"
             };
			/* var formulaArray=["sum","lim","prod","coprod","cos","int","oint","tan","cot","sin"];
			 for(var i=0;i<formulaArray.length;i++){
				if(latex.indexOf(formulaArray[i]) !=-1)
				{
					var r = confirm("Bạn có muốn sử dụng giới hạn ? ");
					if (r == true) {
						
						 break;
					}
				}
			 }    */
			 var re = new RegExp(Object.keys(mapObj).join("|"), "gi");
						 latex = latex.replace(re, function (matched) {
							 return mapObj[matched];
						 });
             latex = '$' + latex + '$' ;
             pasteHtmlAtCaret(latex, el);
             checkmatrix = i;
         }
         clearValue();
     }
     //

 function clearInsert() {
     isSave = false;
     $('#display').attr('contenteditable', 'true');
     $('#display').text('');
 }

 function clearValue() {
     $('#latex-source').val(' ');
     $('#editable-math').mathquill('latex', '');
 }
